package com.jblank.action.business;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jblank.common.config.QueryConfig;
import com.jblank.common.config.SaveConfig;
import com.jblank.common.utils.BusinessUtils;
import com.jblank.common.utils.JsonHelper;
import com.jblank.core.action.BizAction;
import com.jblank.dto.DTO;
import com.jblank.dto.impl.BaseDTO;

/**
 * 公用的action，暴露action服务
 * @author kim
 * @since 2013-07-23
 */
@Component("businessAction")
@Scope("prototype")
public class BusinessAction extends BizAction {

	private static final long serialVersionUID = 1L;
	
	private boolean check(){
		return false;
	}
	
	public String insert() {
		return SUCCESS;
	}
	
	public String update() {
		return SUCCESS;
	}
	
	public String delete() {
		return SUCCESS;
	}
	
	public String save() throws Exception {
		SaveConfig saveConfig = SaveConfig.transDtoToConfig(dto);
		boolean isSuccess = dao.insert(saveConfig);
		DTO resDto = new BaseDTO();
		resDto.setSuccess(isSuccess);
		String jsonString = JsonHelper.encodeObject2Json(resDto);
		write(jsonString, response);
		return null;
	}
	public String queryPage() throws Exception{
		QueryConfig queryConfig = QueryConfig.transDtoToConfig(dto);
		List datas = dao.query(queryConfig);
		int count = dao.queryCount(queryConfig);
		write(encodeList2PageJson(datas,count,queryConfig.getFlds().split(",")),response);
		return null;
	}
	
	public String queryTree() throws Exception{
		String result = BusinessUtils.queryTree(dto,dao);
		write(result,response);
		return null;
	}

}
